package resp

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

type Response[T any] struct {
	Code    int    `json:"code"`
	Data    T      `json:"data"`
	Message string `json:"message"`
}

const (
	ERROR   = 500
	SUCCESS = 0
)

func Result[T any](c *gin.Context, code int, data T, msg string) {
	c.JSON(http.StatusOK, Response[T]{
		Code:    code,
		Data:    data,
		Message: msg,
	})
}

func Ok(c *gin.Context) {
	Result(c, SUCCESS, "", "操作成功")
}

func OkWithMessage(c *gin.Context, message string) {
	Result(c, SUCCESS, "", message)
}

func OkWithData[T any](c *gin.Context, data T) {
	Result(c, SUCCESS, data, "操作成功")
}

func OkWithDetail[T any](c *gin.Context, data T, message string) {
	Result(c, SUCCESS, data, message)
}

func Fail(c *gin.Context) {
	Result(c, ERROR, "", "操作失败")
}

func FailWithMSG(c *gin.Context, message string) {
	Result(c, ERROR, "", message)
}

func FailWithDetail[T any](c *gin.Context, data T, message string) {
	Result(c, ERROR, data, message)
}
